package com.muyu.common.security.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.muyu.common.security.utils.SecurityUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 实体类字段填充
 */
@Log4j2
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    public MyMetaObjectHandler () {
        log.info("mybatis-plus 系统字段填充拦截器 初始化成功");
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", SecurityUtils.getUsername(), metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", SecurityUtils.getUsername(), metaObject);
        this.setFieldValByName("updateTime", new Date(),metaObject);
    }
}
